const express  = require ('express');
const pool = require('../pool');
const jwt = require("jsonwebtoken");
const JWT_SECRET_KEY = "JWT_SECRET_KEY";
const session = require('express-session');
const router = express.Router();


router.post('/login',(req,res,next)=>{
    let obj = req.body;
    console.log(obj);
    pool.query(`select * from user where email=? or phone=? and pwd=${obj.password}`,[obj.username,obj.username],async(err,r)=>{
        if(err){
            return next(err)
        };
        console.log(r)
        if(r.length !=0){

            req.session.nickname = r[0].nickname;
            req.session.u_id = r[0].u_id;
            req.session.uicon = r[0].uicon;
            console.log(req.session)
            let user =  {u_id:r[0].u_id,nickname:r[0].nickname,uicon:r[0].uicon,phone:r[0].phone,email:r[0].email,birthday:r[0].birthday,sex:r[0].sex,desc:r[0].u_desc};
            let token = jwt.sign(user,JWT_SECRET_KEY,{expiresIn:'7d'})
            res.send({code:200,msg:'登陆成功',data:{user,token}})
        }else{
            // pool.query(`select * from user phone=${obj.username} and pwd=?`,[obj.password],async(err,resp)=>{
            //     if(err){
            //         return next(err)
            //     }
            //     if(resp.length !=0){
            //         let user =  {u_id:r[0].u_id,nickname:r[0].nickname,uicon:r[0].uicon};
            //         let token = jwt.sign(user,SECRET_KEY,{expiresIn:'7d'})
            //         res.send({code:200,msg:'登陆成功',data:{user,token}})
            //     }else{
            //         res.send({code:400,msg:'登陆失败，账号密码输入错误不存在'})
            //     }
            // })
            res.send({code:400,msg:'登陆失败，账号或密码输入错误'})
        }
       
    })
})

router.get('/query',(req,res,next)=>{
   
   
    let uid = req.query.id;
    pool.query(`select  *  from  user where u_id=${uid}`,[uid],(err,r)=>{
        if(err){
            return next(err);
        }
        console.log(r)
        res.send({code:200,msg:r[0]})
    })
})

router.get('/update',(req,res,next)=>{
    let name = req.query.name;
    let phone = req.query.phone;
    let email = req.query.email;
    let sex = req.query.sex;
    let time = req.query.time;
    pool.query('update user set nickname=?,phone=?,email=?,sex=?',[name,phone,email,sex],(err,r)=>{
        if(err){
            return next(err)
        }
        console.log(r)
        pool.query('select nickname,phone,email,sex,birthday,u_desc,uicon from user where nickname = ?',[name],(err,resp)=>{
            if(err){
                return next(err)
            }
            if(resp.length !=0){
                let user =  {u_id:resp[0].u_id,nickname:resp[0].nickname,uicon:resp[0].uicon,phone:resp[0].phone,email:resp[0].email,birthday:resp[0].birthday,sex:resp[0].sex,desc:resp[0].u_desc};
                res.send({code:200,msg:'修改成功',data:user})
            }else{
                res.sned({code:400,msg:'修改失败'})
            }
        })
    })
})

module.exports = router;